package com.enjoy.xbase.xui.list;

import android.content.Context;
import android.util.AttributeSet;

import com.enjoy.xbase.xui.adapter.BaseRvViewHolder;
import com.enjoy.xbase.xui.listener.ILoadRefresh;

public class XXSuperListView extends XXBaseListView {
    private EachInter eachInter;

    public XXSuperListView(Context context) {
        super(context);
    }

    public XXSuperListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public XXSuperListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 描述：设置交互器
     *    设置完成后需调用方法loadData加载数据
     * @param eachInter
     */
    public void setEachInter(EachInter eachInter) {
        this.eachInter = eachInter;
        if (this.eachInter==null)return;
        refreshLayout.setEnableRefresh(this.eachInter.canRefresh());
        refreshLayout.setEnableLoadMore(this.eachInter.canLoadMore());
        loadView.setLoadRefresh(new ILoadRefresh() {
            @Override
            public void onRefresh() {
                refreshAndLoad();
            }
        });
    }

    @Override
    protected boolean autoLoadData() {
        return false;
    }

    @Override
    protected boolean canRefresh() {
        if (this.eachInter==null){
            return super.canRefresh();
        }else{
            return this.eachInter.canRefresh();
        }
    }

    @Override
    protected boolean canLoadMore() {
        if (this.eachInter==null){
            return super.canLoadMore();
        }else{
            return this.eachInter.canLoadMore();
        }
    }

    /**
     * 执行加载数据
     */
    public void loadData(){
        refreshAndLoad();
    }
    @Override
    protected int getItemFooterLayoutId() {
        return eachInter.getItemFooterLayoutId();
    }
    @Override
    protected void requestData(int page) {
        eachInter.requestData(page);
    }

    @Override
    public int getItemViewType(int position) {
        return eachInter.getItemViewType(position);
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return eachInter.getItemLayoutId(viewType);
    }

    @Override
    public void onBindViewHolder(BaseRvViewHolder holder, int position) {
        eachInter.onBindViewHolder(holder,position);
    }
    public static abstract class EachInter {
        protected boolean canRefresh() {
            return false;
        }
        protected boolean canLoadMore() {
            return false;
        }
        protected int getItemFooterLayoutId(){
            return -1;
        }
        protected abstract void requestData(int page);
        protected int getItemViewType(int position) {
            return 0;
        }
        protected abstract int getItemLayoutId(int viewType);
        protected abstract void onBindViewHolder(BaseRvViewHolder holder, int position);
    }
}
